package com.bristua.mall.order.biz;

import com.bristua.mall.common.utils.CacheUtil;
import com.bristua.mall.common.utils.DateUtil;
import com.bristua.mall.common.utils.JsonUtil;
import com.bristua.mall.common.utils.SequenceUtil;
import com.bristua.mall.order.dao.TOrderMapper;
import com.bristua.mall.order.dao.TUserEvaluateMapper;
import com.bristua.mall.order.model.TOrder;
import com.bristua.mall.order.model.TUserEvaluate;
import com.bristua.mall.order.model.condition.TUserEvaluateExample;
import com.bristua.mall.order.model.condition.goodsEvaluate.GoodsEvaluate;
import com.bristua.mall.order.model.condition.goodsEvaluate.OrderEvaluate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/24.
 */
@Service
@Transactional
public class OrderGoodsEvaluateBiz {

    @Resource
    private TUserEvaluateMapper tUserEvaluateMapper;

    @Resource
    private TOrderMapper tOrderMapper;

    @Autowired
    private CacheUtil cacheUtil;

    public void orderGoodsEvaluate(OrderEvaluate orderEvaluate){
        Object userSession = cacheUtil.getUserSession();
        Map map = JsonUtil.json2Map(JsonUtil.toJson(userSession));
        List<GoodsEvaluate> goodsEvaluates = orderEvaluate.getGoodsEvaluates();
        String orderId = orderEvaluate.getOrderId();
        if(!StringUtils.isEmpty(orderId)){
            TOrder tOrder = tOrderMapper.selectByPrimaryKey(orderId);
            if(tOrder!=null){
                if(goodsEvaluates !=null && goodsEvaluates.size()>0){
                    List<TUserEvaluate> tUserEvaluates = new ArrayList<>();
                    for(GoodsEvaluate goodsEvaluate :goodsEvaluates){
                        TUserEvaluate tUserEvaluate = new TUserEvaluate();
                        BeanUtils.copyProperties(goodsEvaluate,tUserEvaluate);
                        tUserEvaluate.setUserId(map.get("userId")+"");
                        tUserEvaluate.setCreateTime(DateUtil.getDateTime3());
                        tUserEvaluate.setUserImgUrl(String.valueOf(map.get("userImgUrl")));
                        tUserEvaluate.setUserNickName(String.valueOf(map.get("nickName")));
                        tUserEvaluate.settUserEvaluateId(SequenceUtil.nextId());
                        tUserEvaluates.add(tUserEvaluate);
                    }
                    tUserEvaluateMapper.insertByBatch(tUserEvaluates);
                }
                TOrder upadteOrderEvaluate = new TOrder();
                upadteOrderEvaluate.setEvaluateStatus("1");
                upadteOrderEvaluate.setOrderId(orderId);
                upadteOrderEvaluate.setOrderStatus("3");
                tOrderMapper.updateByPrimaryKeySelective(upadteOrderEvaluate);

            }
        }

    }
}
